package org.dstart.draw.cmanage
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import org.dstart.draw.cstruct.CObjectPlace;
	import org.dstart.draw.csuper.CSprite;
	
	public final class CSpaceManage extends CSprite
	{
		private var placeModel:Number;		//	容器内对象的对齐模式
		private var moveRect:Rectangle;		//	容器的可管理范围	
		private var objArray:Array;			//	要管理的对象列表（对象含一个 DisplayObject 对象和一个方位值）
		
		public function CSpaceManage(r:Rectangle, m:Number = 0)
		{
			objArray = new Array();
			
			this.rect = r;
			this.model = m;
		}
		
		//	添加一个对象
		public function AddObject(spr:DisplayObject, basePoint:Point = null):void
		{
			basePoint = (basePoint) ? basePoint : new Point(0, 0);
			objArray.push(new CObjectPlace(spr, basePoint));
						
			RushObjectPlace();
		}
		
		//	删除一个对象
		public function RemoveObject(spr:DisplayObject):void
		{
			for(var i:int = 0; i < objArray.length; i++)
			{
				var tc:CObjectPlace = objArray[i];
				
				if(tc.sprObject.name == spr.name)
				{
					removeChild(tc.sprObject);
					objArray.splice(i, 1);
				}
			}
			
			RushObjectPlace();
		}
		
		//	设置容器的范围
		public function set rect(rect:Rectangle):void
		{
			moveRect = rect;
			
			x = rect.x;
			y = rect.y;
		}
		
		//	读取窗口的范围
		public function get rect():Rectangle
		{
			return moveRect;
		}
		
		//	设置容器对齐方式
		public function set model(num:Number):void
		{
			this.placeModel = CheckModel(num); 
		}
		
		//------------------------------------------------------------
		
		private function CheckModel(num:Number):Number
		{
			return 0;
		}
		
		private function RushObjectPlace():void
		{
			for(var i:int = 0; i < objArray.length; i++)
			{
				var tc:CObjectPlace = objArray[i];				
				
				addChild(tc.sprObject);
			}
		}
	}
}